<?php
namespace app\api\controller;
use app\common\controller\BaseApi;
/*卡api*/
class Card extends  BaseApi
{
	/**
	 * 测试地址
	 * http://tp5.com/public/api/Card/add_card/v/1/t/1/key/zwzl_admin/sign/5de093255ad3e78341dbe00309c9436c
	 * */
	 public function _initialize()
    {
		$array=array("add_member_card","statistic","statistic1","search");
    	if(!in_array(request()->action(),$array)){
    		parent::_initialize();	
    	}
        
    }
	/**
	 * 写入卡数据
	 * */
	public  function add_card(){
		$flag=$this->validate(input(''),'card');
		if($flag!==true){//参数验证错误
			return $this->ajax_error('',"0003",$flag);
		}else{
			model('Card')->create(input(''),TRUE);
			return $this->ajax_success();
		}
	}
	/*修改卡*/
	public function update_card(){
		$card_no=input("card_no/s","0");
		$data=db('card')->where("card_no1='$card_no'")->find();
		if(empty($data)){
			return $this->ajax_error('',"0004",config("err_code.0004"));
		}else{
			$data['type']=input("type/d",$data['type']);
			$data['owner_type']=input("owner_type/d",$data['owner_type']);
			$data['fingerprint']=input("fingerprint/s",$data['fingerprint']);	
			db('Card')->update($data);
			return $this->ajax_success();		
		}
	}
	/*删除卡*/
	public function delete_card(){
		$card_no=input("card_no/s","0");
		db("card")->where("card_no1='$card_no'")->delete();
		db("interface")->where("card_no1='$card_no'")->delete();
		return $this->ajax_success();
	}
	
	/*获取当前刷卡次数，和人次统计*/
	public function  statistics(){
		$data=input('date');//date('Y-m-d');
		$result='';
		//人流量
		$result1=db('BehaviorLog')->field("card_no,count(1) as count")->where("behavior_status=1 and date_format(create_time,'%Y-%m-%d')='$data' ")->group("card_no")->select();
		//进场次数
		$result2=db('BehaviorLog')->field("count(1) as count")->where("behavior_status=1 and date_format(create_time,'%Y-%m-%d')='$data' ")->find();
		
		if(empty($result1)){
			$result['people']=0;	
		}else{
			$result['people']=count($result1);
		}
		
		if(empty($result2)){
			$result['frequence']=0;	
		}else{
			$result['frequence']=$result2['count'];
		}
//		var_dump($result);die;
		return $this->ajax_success($result);
	}
	
////////////////////////////////////////////////////////////////////////////////////
	public function test(){
		$key=config('auth_key.0');
		var_dump($key."_"."1");//zwzl_admin_1
		var_dump(md5($key."_"."1"));//5de093255ad3e78341dbe00309c9436c
	}
	//验证
	public function validates(){
		var_dump($this->validate(input(),'Card'));
	}
//	/**/
//	public function test(){
//		db("card")->count(1);
//		var_dump("");
//	}
	
	/**
	 * 新增员工卡
	 *  http://tp5.com/api/Card/add_member_card?card_nos=11111,22222
	 * */
	public function add_member_card(){
		$card_nos=db("Card")->column("card_no1");
		$data=explode(",", input("card_nos"));
		$success='';
		foreach ($data as $key => $value) {
			$temp_key=str_pad(trim($value), 10,'0',STR_PAD_LEFT);
			if(in_array($temp_key, $card_nos)||empty($value)){
				continue;
			}
			$card_no_=$this->to_hex_revers($temp_key);
			db("Card")->insert(array("card_no"=>$card_no_,"card_no1"=>$temp_key,"owner_type"=>2));
			db("Interface")->insert(array("card_no"=>$card_no_,"card_no1"=>$temp_key,"start_date"=>"2017-01-01","effect_date"=>"2099-11-11"));
			$success[]=$value;
		}
		return $this->ajax_success();		
	}
	public function search(){
		$card_no=input('card_nos');
		$count=db("card")->where("card_no1='$card_no'")->count(1);
		if(!empty($count)){
				return $this->ajax_success("卡存在");		
		}else{
			return $this->ajax_success("抱歉 无法查询到卡号");			
		}
		
	}
	
	
	public function statistic(){
		$card_no=input('card_nos');
		$result='';
		$data=db("card")->field("owner_type,count(1) as count")->group("owner_type")->select();
		if(empty($data)){
			return $this->ajax_success("抱歉 数据库 没有数据");		
		}else{
			foreach($data as $key =>$value){
				if($value['owner_type']==1){
					$result.=",当前会员卡数量：".$value['count'];
				}else if($value['owner_type']==2){					
					$result.=",当前员工卡数量：".$value['count'];
				
				}
			}
		}
		return $this->ajax_success($result);						
	}
	
	public function statistic1(){
		$data=date('Y-m-d');
		$result=db('BehaviorLog')->field("card_no,count(1) as count")->where("behavior_status=1 and date_format(create_time,'%Y-%m-%d')='$data' ")->group("card_no")->select();
		if(empty($result)){
			return $this->ajax_success("当前人流量：0");	
		}else{
			return $this->ajax_success("当前人流量：".count($result));	
		}
								
	}
	
	//参数转对象
	public function toObj(){
		 $user=(new \Card(input('')))->getData();
//		 var_dump($user);die;
//		// 验证参数
//		$this->validate(input(''),'Card')
//		//保存后获取最后一条id
//		$last_id=model('Card')->create(input(''),TRUE)->getLastInsID();//保存并获取id
//		//将参数转为对象新增到数据库 并且返回参数，包含新增后的id
//		$data= model('Card')->create(input(''),TRUE)->getData();
	}
}
